package com.wude.guild.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.AboutUs;
import com.wude.guild.R;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版本更新的Dialog
 * Created by Administrator on 2017/3/15.
 */

public class VersionDialog extends Dialog {
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.spend)
    TextView spend;
    @BindView(R.id.size)
    TextView size;
    @BindView(R.id.progressbar)
    RoundCornerProgressBar progressbar;
    @BindView(R.id.down_layout)
    RelativeLayout downLayout;
    @BindView(R.id.tv_versionLog)
    TextView tv_versionLog;
    private View inflate;
    private String name;
    private String neirong;
    private View.OnClickListener dianji;
    private boolean niu = true;
    private String versionLog;

    public VersionDialog(Context context, int themeResId, String versionLog) {
        super(context, themeResId);
        this.versionLog = versionLog;
        inflate = LinearLayout.inflate(context, R.layout.updata_app_version, null);
    }

    public VersionDialog(Context context) {
        super(context);
    }

    protected VersionDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(inflate);
        ButterKnife.bind(this);

        tv_versionLog.setText(versionLog + "");
    }

    @OnClick({R.id.cancel, R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                System.exit(0);
                break;
            case R.id.ok:
                DbManager db = DbUtils.getDB();
                try {
                    AboutUs first = db.findFirst(AboutUs.class);
                    if (first != null) {
                        if (first.versionUrl != null && !first.versionUrl.equals("")) {
                            StartDown(first.versionUrl);
                        } else {
                            Log.e("下载链接为空", "下载链接为空");
                            System.exit(0);
                        }
                    } else {
                        Log.e("更新失败", "更新失败");
                        System.exit(0);
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                    Log.e("更新失败", e.toString());
                }
                break;
        }
    }


    //下载
    protected void StartDown(String url) {
        ok.setClickable(false);
        RequestParams entity = new RequestParams(url);
        entity.setAutoResume(true);//是否断点下载
        entity.setAutoRename(false);//下载完成后是否自动重命名
        entity.setCancelFast(true);//快速取消
        entity.setSaveFilePath(Utils.getApkFile(String.valueOf(00003)).getAbsolutePath());//文件保存路径
        x.http().get(entity, new Callback.ProgressCallback<File>() {

            @Override
            public void onCancelled(CancelledException arg0) {
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                ToastUtil.showToast("下载失败");
                System.exit(0);
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onSuccess(File arg0) {
                File apkFile = Utils.getApkFile(String.valueOf(00003));
                Utils.installApp(Utils.getContext(), apkFile);
            }

            @Override
            public void onLoading(long arg0, long arg1, boolean arg2) {
                int progress = (int) (arg1 * 100f / arg0 + 0.5f);
                String spent = Utils.getSpent(arg1);
                String size1 = Utils.getSize(arg1, arg0);
                spend.setText(spent);
                size.setText(size1);
                progressbar.setProgress(progress);
            }

            @Override
            public void onStarted() {

            }

            @Override
            public void onWaiting() {
                spend.setText("0kb/s");
                size.setText("0M/0M");
                progressbar.setProgress(0);
            }
        });
    }
}
